package service.impl;

import bean.CutPageBean;
import bean.RoomBean;
import mapper.IClassMapper;
import mapper.IRoomMapper;
import service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoomServiceImpl extends BaseService implements IRoomService {
    @Override
    public List<RoomBean> findBySex(String sex) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> list = mapper.findBySex(sex);
        session.close();
        return list;
    }

    @Override
    public void insert(RoomBean roomBean) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.insert(roomBean);
        session.commit();
        session.close();
    }

    @Override
    public boolean delete(Integer id) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        int count = mapper.roomStudentCount(id);
        if (count == 0) {
            mapper.delete(id);
            session.commit();
            session.close();
            return true;
        } else {
            return false;
        }
    }

    @Override
    public RoomBean getById(int id) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean roomBean = mapper.getById(id);
        session.close();
        return roomBean;
    }

    @Override
    public CutPageBean<RoomBean> listStudentByPage(int current, String roomAddr, String roomStatus, String roomHabitable, String type) {
        CutPageBean cutPageBean = new CutPageBean();
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        cutPageBean.setList(mapper.listStudentByPage((current - 1) * CutPageBean.PAGESIZE, CutPageBean.PAGESIZE, roomAddr, roomStatus, roomHabitable, type));
        cutPageBean.setCount(mapper.cutPageCount(roomAddr, roomStatus, roomHabitable, type));
        return cutPageBean;
    }

    @Override
    public void updateStatus(Integer roomId, String roomStatus) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.updateStatus(roomId, roomStatus);
        session.commit();
        session.close();
    }
}
